package org.gushi.datastruct.a06;

import org.gushi.datastruct.a06.printer.BinaryTrees;

import java.util.Comparator;

public class TreeMainTest {

    static void test1(){
        Integer[] integers = new Integer[]{7,4,9,2,5,8,11,3};

        BinarySearchTree<Integer> binarySearchTree = new BinarySearchTree();

        for (int i = 0; i < integers.length; i++) {
            binarySearchTree.add(integers[i]);
        }

        BinaryTrees.print(binarySearchTree);
    }

    static void test2(){
        Integer[] integers = new Integer[]{7,4,9,2,5,8,11,3};

        BinarySearchTree<Person> binarySearchTree = new BinarySearchTree();

        for (int i = 0; i < integers.length; i++) {
            binarySearchTree.add(new Person(integers[i]));
        }

        BinaryTrees.print(binarySearchTree);
    }

    /**
     * 测试前序遍历
     */
    static void test3(){
        Integer[] integers = new Integer[]{7,4,9,2,5,8,11,3};

        BinarySearchTree<Person> binarySearchTree = new BinarySearchTree();

        for (int i = 0; i < integers.length; i++) {
            binarySearchTree.add(new Person(integers[i]));
        }

        BinaryTrees.print(binarySearchTree);

        binarySearchTree.preorderTraversal();
    }
    /**
     * 测试中序遍历
     */
    static void test4(){
        Integer[] integers = new Integer[]{7,4,9,2,5,8,11,3,1};

        BinarySearchTree<Person> binarySearchTree = new BinarySearchTree();

        for (int i = 0; i < integers.length; i++) {
            binarySearchTree.add(new Person(integers[i]));
        }

        BinaryTrees.print(binarySearchTree);

        System.out.println();
        binarySearchTree.inorderTraversal();
    }
    /**
     * 测试后序遍历
     */
    static void test5(){
        Integer[] integers = new Integer[]{7,4,9,2,5,8,11,3,1};

        BinarySearchTree<Person> binarySearchTree = new BinarySearchTree();

        for (int i = 0; i < integers.length; i++) {
            binarySearchTree.add(new Person(integers[i]));
        }

        BinaryTrees.print(binarySearchTree);

        System.out.println();
        binarySearchTree.lastOrderTraversal();
    }

    /**
     * 层序遍历测试
     */
    static void test6(){
        Integer[] integers = new Integer[]{7,4,9,2,5,8,11,3};

        BinarySearchTree<Person> binarySearchTree = new BinarySearchTree();

        for (int i = 0; i < integers.length; i++) {
            binarySearchTree.add(new Person(integers[i]));
        }

        BinaryTrees.print(binarySearchTree);

        System.out.println();
        binarySearchTree.levelOrderTraversal();
    }

    /**
     * 测试 -- 访问者模式 外界自定义元素处理逻辑
     */
    static void test7(){
        Integer[] integers = new Integer[]{7,4,9,2,5,8,11,3};

        BinarySearchTree<Person> binarySearchTree = new BinarySearchTree();

        for (int i = 0; i < integers.length; i++) {
            binarySearchTree.add(new Person(integers[i]));
        }

        BinaryTrees.print(binarySearchTree);

        System.out.println();
        binarySearchTree.levelOrder(new BinarySearchTree.Visitor<Person>() {
            @Override
            public boolean visit(Person element) {
                System.out.println("--"+element.getAge());
                return false;
            }
        });
    }

    static void test8(){
        Integer[] integers = new Integer[]{7,4,9,2,5};

        BinarySearchTree<Person> binarySearchTree = new BinarySearchTree();

        for (int i = 0; i < integers.length; i++) {
            binarySearchTree.add(new Person(integers[i]));
        }

        BinaryTrees.print(binarySearchTree);

        System.out.println();
        binarySearchTree.preorder(new BinarySearchTree.Visitor<Person>() {
            @Override
            public boolean visit(Person element) {
                System.out.print("--"+element.getAge());
                return false;
            }
        });

        binarySearchTree.inorder(new BinarySearchTree.Visitor<Person>() {
            @Override
            public boolean visit(Person element) {
                System.out.print("--"+element.getAge());
                return false;
            }
        });

        binarySearchTree.lastOrder(new BinarySearchTree.Visitor<Person>() {
            @Override
            public boolean visit(Person element) {
                System.out.print("--"+element.getAge());
                return false;
            }
        });
    }


    /**
     * 层序遍历测试
     */
    static void test9(){
        Integer[] integers = new Integer[]{7,4,9,2,5,8,11,3};

        BinarySearchTree<Integer> binarySearchTree = new BinarySearchTree();

        for (int i = 0; i < integers.length; i++) {
            binarySearchTree.add(integers[i]);
        }

        BinaryTrees.print(binarySearchTree);
        binarySearchTree.remove(7);
//        binarySearchTree.remove(5);


        System.out.println("------------------------------------------------------");

        BinaryTrees.print(binarySearchTree);
    }

    public static void main(String[] args) {

//        test1();
//        test2();
//        test3();
//        test4();
//        test5();

//        test6();
//        test7();
        test9();

    }



    private static class MyComparator implements Comparator<Person> {
        @Override
        public int compare(Person e1, Person e2) {
            if(e1.getAge()>e2.getAge()){
                return 1;
            }else if(e1.getAge()<e2.getAge()){
                return -1;
            }
            return 0;
        }
    }


}
